//
//  VisibleStudentListViewController.m
//  家校互通
//
//  Created by 李赛 on 15/6/14.
//  Copyright (c) 2015年 姬鑫宇. All rights reserved.
//

#import "VisibleStudentListViewController.h"
#import "VisibleStudentTableViewCell.h"
#import "CoModel.h"
@interface VisibleStudentListViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *studentList;

@property (nonatomic, strong) NSMutableArray *selectStudentArray;
@end

@implementation VisibleStudentListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _contentTableView.backgroundColor=[UIColor clearColor];
    _contentTableView.delegate=self;
    _contentTableView.dataSource=self;
    
//    _contentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _contentTableView.allowsMultipleSelectionDuringEditing=YES;
    _contentTableView.tintColor =COLOR(50, 150, 150);   //选中对号为红色
    // Do any additional setup after loading the view.
    [_contentTableView setEditing:YES animated:YES];
    _studentList=[[NSMutableArray alloc]initWithCapacity:0];
    _selectStudentArray=[[NSMutableArray alloc]initWithCapacity:0];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneForSelectStudent)];
    [self connect];
}

- (void)connect{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
   [CoModel getClazzStudents:^(BOOL result, NSMutableArray *roomList, NSError *error)
    {
        if (result) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            _studentList=[NSMutableArray arrayWithArray:roomList];
            [_contentTableView reloadData];
        }
       
   } withToken:[DSFUserInfo shareUserInstance].token andClassId:[DSFUserInfo shareUserInstance].clazz_user.clazz.id];
}

- (void)doneForSelectStudent
{
    if (_senderUsers!=NULL) {
       _senderUsers(_selectStudentArray);
    }
    _senderUsers = nil;//Importent: 在使用之后将Block赋空值，解引用 !!!
    [self.navigationController popViewControllerAnimated:YES];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

#pragma mark UITableViewDelegate--
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    return nil;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if ([self.title isEqualToString:@"选择要表彰的人"])
    {
        if (_selectStudentArray.count==0) {
            ClazzStudents *model=[_studentList objectAtIndex:indexPath.row];
            [_selectStudentArray addObject:model];
            [_contentTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
        }else
        {
            HHAlertView *alertview = [[HHAlertView alloc] initWithTitle:@"温馨提示" detailText:@"您已经选择了要表彰的同学，每次最多能表彰一位同学~" addView:self.view cancelButtonTitle:nil otherButtonTitles:@[@"确定"]];
            alertview.mode = HHAlertViewModeDefault;
            [alertview showWithBlock:^(NSInteger index) {
   
                return;
            }];
//            UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"您已经选择了要表彰的同学，每次最多能表彰一位同学~" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
//            [alertView show];
             [_contentTableView deselectRowAtIndexPath:indexPath animated:YES];
        }
       
    }
    else
    {
        ClazzStudents *model=[_studentList objectAtIndex:indexPath.row];
        [_selectStudentArray addObject:model];
        [_contentTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    }
   
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.title isEqualToString:@"选择要表彰的人"])
    {
        ClazzStudents *model=[_studentList objectAtIndex:indexPath.row];
        [_selectStudentArray removeObject:model];
         [_contentTableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    else
    {
        ClazzStudents *model=[_studentList objectAtIndex:indexPath.row];
        [_selectStudentArray removeObject:model];
        [_contentTableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    
}

//修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

//单元格返回的编辑风格，包括删除 添加 和 默认  和不可编辑三种风格
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return UITableViewCellEditingStyleDelete;
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
       
        
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }
}
#pragma mark UITableViewDateSource--
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    VisibleStudentTableViewCell *vCell=[tableView dequeueReusableCellWithIdentifier:@"VisibleStudentTableViewCell" forIndexPath:indexPath];
    ClazzStudents *model=[_studentList objectAtIndex:indexPath.row];
    vCell.titleLb.text=model.name;
        //去除选中时的背景颜色
    UIView *selectView=[[UIView alloc]initWithFrame:vCell.frame];
    selectView.backgroundColor=[UIColor clearColor];
    vCell.multipleSelectionBackgroundView=selectView;
    
   // [_contentTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
 
    return vCell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _studentList.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
    
}

-(BOOL)shouldAutorotate
{
    return NO;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
